package com.example.StudentComponent;

import com.example.Utils.HttpUtil;
import com.example.Utils.ResultUtil;
import com.example.pojo.ParseStudent;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;

import java.util.Vector;

public class Score extends Box{

    JTable Table;
    Vector<String> student=new Vector<>();
    Vector<Vector<String>> students=new Vector<Vector<String>>();
    TableModel tm;


    //构造方法
    public Score(String user) {
        super(BoxLayout.Y_AXIS);
        //组装
        JPanel jPanel = new JPanel();
        jPanel.setBackground(new Color(208, 210, 213, 179));
        jPanel.setMaximumSize(new Dimension(700, 50));


        //组装表格
        String[] ss = {"学号", "姓名", "班级", "课程", "成绩"};
        for (int i = 0; i < ss.length; i++) {
            student.add(ss[i]);
        }
        requestGetData("http://localhost:8080/select?id="+user);
        tm = new DefaultTableModel(students, student);
        Table = new JTable(tm) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        //只能选中一行
        Table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();//单元格渲染器
        tcr.setHorizontalAlignment(JLabel.CENTER);//居中显示
        Table.setDefaultRenderer(Object.class, tcr);//设置渲染器
        JScrollPane scrollPane = new JScrollPane(Table);
        Table.setFont(new Font("宋体", Font.PLAIN, 15));
        this.add(scrollPane);
    }





    public  void requestGetData( String url) {
        String result = HttpUtil.sendGet(url);
        Vector<ParseStudent> parseStudents = ResultUtil.ResultVectorStudent(result);
        Vector<Vector<String>> ss = new Vector<>();
        for (int i = 0; i < parseStudents.size(); i++) {
            Vector<String> s = new Vector<>();
            s.add(parseStudents.get(i).getId());
            s.add(parseStudents.get(i).getName());
            s.add(parseStudents.get(i).getAaClass());
            s.add(parseStudents.get(i).getCourse());
            s.add(String.valueOf(parseStudents.get(i).getScore()));
            ss.add(s);
        }
        students.clear();
        for (Vector<String> sv  : ss) {
            students.add(sv);
        }

    }


}
